package com.happystreet.config
{
	import com.happystreet.GlobalGameConstants;
	import com.happystreet.info.BackgroundInfo;
	import com.happystreet.info.ItemInfo;
	import com.happystreet.info.WallInfo;
	import com.happystreet.utils.CustomUtils;

	/**
	 * 道具配置 item.xml
	 * @author Liseen
	 * 
	 */	
	public class AssetsConfig
	{
		public static const levelRequiredToUnlockRecentItems:int = 6;
		
		private static var itemTable:Array;
		public function AssetsConfig()
		{
			throw new Error("AssetsConfig: this class can't be instantiated.");
		}
		
		/**
		 * 获取更新的物品
		 * @return Array
		 * 
		 */		
		public static function getUpdateItems():Array
		{
			var itemInfo:ItemInfo;
			var list:Array = new Array();
			for each (itemInfo in itemTable)
			{
				if (itemInfo.isInUpdatePopUp)
				{
					list.push(itemInfo);
				}
			}
			list = CustomUtils.randomizeArray(list);
			return list;
		}
		
		/**
		 *随即设置多少个item为新道具 
		 * @param range		设置多少个
		 * 
		 */		
		public static function setRandomNewItems(range:int=20):void
		{
			var randomIndex:int;
			while (range > 0)
			{
				randomIndex = Math.floor(Math.random() * itemTable.length);
				itemTable[randomIndex].isNew = true;
				range--;
			}
		}
		
		/**
		 *随即设置多少个item促销 
		 * @param rang
		 * 
		 */		
		public static function setRandomSaleItems(rang:int=10):void
		{
			var randomIndex:int;
			while (rang > 0)
			{
				randomIndex = Math.floor(Math.random() * itemTable.length);
				itemTable[randomIndex].isSale = true;
				itemTable[randomIndex].salesPriceInCash = Math.floor(itemTable[randomIndex].priceInCash * 0.2);
				itemTable[randomIndex].salesPriceInCoins = Math.floor(itemTable[randomIndex].priceInCoins * 0.2);
				rang--;
			}
		}
		
		/**
		 *根据类型和shop类型来获取物品id列表 <当前还没完全实现这方法>
		 * @param itemType
		 * @param shopType
		 * @param currentFilter
		 * @return 
		 * 
		 */		
		public static function getItemIdsByTypeAndShop(itemType:uint, shopType:uint, currentFilter:int=0):Array
		{
			var itemInfo:ItemInfo;
			var idList:Array = new Array();
			for each (itemInfo in itemTable)
			{
				if (itemInfo.type == itemType){
					if (!(!(itemInfo.isInStore) && !(currentFilter == GlobalGameConstants.WAREHOUSE_FILTER))){
						
					}
				}
			}
			
			return idList;
		}
		
		/**
		 *设置物品配置数据
		 * 根据配置表内的type类型进行对应的处理
		 * @param data
		 * 
		 */		
		public static function setAssetsConfig(data:String):void
		{
			var itemType:XML;
			var name:String;
			var type:int;
			var config:XML = new XML(data);
			itemTable = new Array();
			for each (itemType in config..itemType)
			{
				name = itemType.@name;
				type = itemType.@type;
				switch (type)
				{
					case GlobalGameConstants.BACKGROUND_TYPE:
						processBackgrounds(itemType..item);
						break;
					case GlobalGameConstants.WALL_SHAPE_TYPE:
						processWalls(itemType..item);
						break;
					default:
						processItems(itemType..item);
				}
			}
			config = null;
		}
		/**
		 *通过道具id获取ItemInfo 
		 * @param id	道具id
		 * @return	ItemInfo 
		 * 
		 */		
		public static function getItemData(id:uint):ItemInfo
		{
			var itemInfo:ItemInfo;
			for each (itemInfo in itemTable)
			{
				if (itemInfo.id == id)
				{
					return itemInfo
				}
			}
			trace("Item " + id + " not found");
			return null;
		}
		
		/**
		 *解析背景 
		 * @param dataList
		 * 
		 */		
		private static function processBackgrounds(itemList:XMLList):void
		{
			var item:XML;
			var backInfo:BackgroundInfo;
			for each (item in itemList)
			{
				backInfo = new BackgroundInfo(item);
				itemTable.push(backInfo);
			}
		}
		
		/**
		 *解析墙壁模型
		 * @param dataList
		 * 
		 */		
		private static function processWalls(dataList:XMLList):void
		{
			var wall:XML;
			var wallInfo:WallInfo;
			for each (wall in dataList)
			{
				wallInfo = new WallInfo(wall);
				itemTable.push(wallInfo);
			}
		}
		
		/**
		 * 解析道具
		 * 
		 */		
		private static function processItems(dataList:XMLList):void
		{
			var item:XML;
			var itemInfo:ItemInfo;
			for each (item in dataList)
			{
				itemInfo = new ItemInfo(item);
				itemTable.push(itemInfo);
			}
		}
		
	}
}